﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Applee.FractCommon
{
    public class RandomColor : List<Color>
    {
        public RandomColor()
        {
            InitRandomColor();
        }

        private void InitRandomColor()
        {
            //Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
            ////  C#的随机数
            //System.Threading.Thread.Sleep(RandomNum_First.Next(50));
            //Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);


            //int int_Red = RandomNum_First.Next(256);
            //int int_Green = RandomNum_Sencond.Next(256);
            //int int_Blue = RandomNum_Sencond.Next(256);
            ////  为了在白色背景上显示，尽量生成深色 
            ////int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
            ////int_Blue = (int_Blue > 255) ? 255 : int_Blue;

            //return Color.FromArgb(255, int_Red, int_Green, int_Blue);

            //Random r = new Random();
            Random r = new Random((int)DateTime.Now.Ticks);
            byte[] buffer = new byte[512];
            r.NextBytes(buffer);

            foreach(byte b in buffer)
            {
                Color color = Color.FromKnownColor((KnownColor)b);
                if(color.GetSaturation() > 0.25 && color.GetBrightness() < 0.85 )
                    Add(color);
            }
        }

        public Color GetColor(int nIndex)
        {
            if (nIndex < 0 || nIndex > Count - 1)
                nIndex = 0;

            return this[nIndex];
        }

        public Color[] GetColor2(int nIndex, int nCount)
        {
            if(nCount == 0)
                return new Color[]{GetColor(nIndex)};

            if (nIndex < 0 || nIndex > Count - 1)
                nIndex = 0;

            int nParts = Count / nCount;
            if (nParts < 1)
                throw new NotSupportedException();

            Color[] colors = new Color[nCount];
            for(int n = 0; n < nCount; n++)
            {
                colors[n] = GetColor(n * nParts + nIndex);
            }

            return colors;
        }
    }
}
